<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">

    <div class="article-intro" id="content">

        <h1>Python3 输入和输出</h1>

        <p>在前面几个章节中，我们其实已经接触了 Python 的输入输出的功能。本章节我们将具体介绍 Python 的输入输出。</p>
        <h2>


        </h2>
        <h2>输出格式美化</h2>
        <p>
            Python两种输出值的方式: 表达式语句和 print() 函数。
        </p>
        <p>第三种方式是使用文件对象的 write() 方法，标准输出文件可以用 sys.stdout 引用。</p>
        <p></p>
        <p>如果你希望输出的形式更加多样，可以使用 str.format() 函数来格式化输出值。</p>
        <p>如果你希望将输出的值转成字符串，可以使用 repr() 或 str() 函数来实现。</p>
        <ul>
            <li>
                <b>str()：</b> 函数返回一个用户易读的表达形式。
            </li>
            <li>
                <b>repr()：</b> 产生一个解释器易读的表达形式。
            </li>
        </ul>
        <h3>
            例如
        </h3>

        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> s </span><span class="pun">=</span><span class="pln"> </span><span class="str">'Hello, Runoob'</span><span
                class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> str</span><span class="pun">(</span><span
                class="pln">s</span><span class="pun">)</span><span class="pln">
</span><span class="str">'Hello, Runoob'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> repr</span><span class="pun">(</span><span
                class="pln">s</span><span class="pun">)</span><span class="pln">
</span><span class="str">"'Hello, Runoob'"</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> str</span><span class="pun">(</span><span
                class="lit">1</span><span class="pun">/</span><span class="lit">7</span><span class="pun">)</span><span
                class="pln">
</span><span class="str">'0.14285714285714285'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> x </span><span class="pun">=</span><span
                class="pln"> </span><span class="lit">10</span><span class="pln"> </span><span class="pun">*</span><span
                class="pln"> </span><span class="lit">3.25</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> y </span><span class="pun">=</span><span
                class="pln"> </span><span class="lit">200</span><span class="pln"> </span><span
                class="pun">*</span><span class="pln"> </span><span class="lit">200</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> s </span><span class="pun">=</span><span
                class="pln"> </span><span class="str">'x 的值为： '</span><span class="pln"> </span><span
                class="pun">+</span><span class="pln"> repr</span><span class="pun">(</span><span
                class="pln">x</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span
                class="pln"> </span><span class="str">',  y 的值为：'</span><span class="pln"> </span><span
                class="pun">+</span><span class="pln"> repr</span><span class="pun">(</span><span
                class="pln">y</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span
                class="pln"> </span><span class="str">'...'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span
                class="pun">(</span><span class="pln">s</span><span class="pun">)</span><span class="pln">
x </span><span class="pun">的值为：</span><span class="pln"> </span><span class="lit">32.5</span><span
                class="pun">,</span><span class="pln">  y </span><span class="pun">的值为：</span><span
                class="lit">40000.</span><span class="pun">..</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="com">#  repr() 函数可以转义字符串中的特殊字符</span><span
                class="pln">
</span><span class="pun">...</span><span class="pln"> hello </span><span class="pun">=</span><span
                class="pln"> </span><span class="str">'hello, runoob\n'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> hellos </span><span class="pun">=</span><span
                class="pln"> repr</span><span class="pun">(</span><span class="pln">hello</span><span
                class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span
                class="pun">(</span><span class="pln">hellos</span><span class="pun">)</span><span class="pln">
</span><span class="str">'hello, runoob\n'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span
                class="com"># repr() 的参数可以是 Python 的任何对象</span><span class="pln">
</span><span class="pun">...</span><span class="pln"> repr</span><span class="pun">((</span><span
                class="pln">x</span><span class="pun">,</span><span class="pln"> y</span><span class="pun">,</span><span
                class="pln"> </span><span class="pun">(</span><span class="str">'Google'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'Runoob'</span><span
                class="pun">)))</span><span class="pln">
</span><span class="str">"(32.5, 40000, ('Google', 'Runoob'))"</span></pre>
        <p>
            这里有两种方式输出一个平方与立方的表:
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">for</span><span class="pln"> x </span><span
                class="kwd">in</span><span class="pln"> range</span><span class="pun">(</span><span class="lit">1</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">11</span><span
                class="pun">):</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     </span><span class="kwd">print</span><span
                class="pun">(</span><span class="pln">repr</span><span class="pun">(</span><span
                class="pln">x</span><span class="pun">).</span><span class="pln">rjust</span><span
                class="pun">(</span><span class="lit">2</span><span class="pun">),</span><span
                class="pln"> repr</span><span class="pun">(</span><span class="pln">x</span><span
                class="pun">*</span><span class="pln">x</span><span class="pun">).</span><span
                class="pln">rjust</span><span class="pun">(</span><span class="lit">3</span><span
                class="pun">),</span><span class="pln"> </span><span class="kwd">end</span><span
                class="pun">=</span><span class="str">' '</span><span class="pun">)</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     </span><span class="com"># 注意前一行 'end' 的使用</span><span
                class="pln">
</span><span class="pun">...</span><span class="pln">     </span><span class="kwd">print</span><span
                class="pun">(</span><span class="pln">repr</span><span class="pun">(</span><span
                class="pln">x</span><span class="pun">*</span><span class="pln">x</span><span class="pun">*</span><span
                class="pln">x</span><span class="pun">).</span><span class="pln">rjust</span><span
                class="pun">(</span><span class="lit">4</span><span class="pun">))</span><span class="pln">
</span><span class="pun">...</span><span class="pln">
 </span><span class="lit">1</span><span class="pln">   </span><span class="lit">1</span><span
                class="pln">    </span><span class="lit">1</span><span class="pln">
 </span><span class="lit">2</span><span class="pln">   </span><span class="lit">4</span><span
                class="pln">    </span><span class="lit">8</span><span class="pln">
 </span><span class="lit">3</span><span class="pln">   </span><span class="lit">9</span><span
                class="pln">   </span><span class="lit">27</span><span class="pln">
 </span><span class="lit">4</span><span class="pln">  </span><span class="lit">16</span><span
                class="pln">   </span><span class="lit">64</span><span class="pln">
 </span><span class="lit">5</span><span class="pln">  </span><span class="lit">25</span><span class="pln">  </span><span
                class="lit">125</span><span class="pln">
 </span><span class="lit">6</span><span class="pln">  </span><span class="lit">36</span><span class="pln">  </span><span
                class="lit">216</span><span class="pln">
 </span><span class="lit">7</span><span class="pln">  </span><span class="lit">49</span><span class="pln">  </span><span
                class="lit">343</span><span class="pln">
 </span><span class="lit">8</span><span class="pln">  </span><span class="lit">64</span><span class="pln">  </span><span
                class="lit">512</span><span class="pln">
 </span><span class="lit">9</span><span class="pln">  </span><span class="lit">81</span><span class="pln">  </span><span
                class="lit">729</span><span class="pln">
</span><span class="lit">10</span><span class="pln"> </span><span class="lit">100</span><span class="pln"> </span><span
                class="lit">1000</span><span class="pln">

</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">for</span><span
                class="pln"> x </span><span class="kwd">in</span><span class="pln"> range</span><span
                class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">11</span><span class="pun">):</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     </span><span class="kwd">print</span><span
                class="pun">(</span><span class="str">'{0:2d} {1:3d} {2:4d}'</span><span class="pun">.</span><span
                class="pln">format</span><span class="pun">(</span><span class="pln">x</span><span
                class="pun">,</span><span class="pln"> x</span><span class="pun">*</span><span class="pln">x</span><span
                class="pun">,</span><span class="pln"> x</span><span class="pun">*</span><span class="pln">x</span><span
                class="pun">*</span><span class="pln">x</span><span class="pun">))</span><span class="pln">
</span><span class="pun">...</span><span class="pln">
 </span><span class="lit">1</span><span class="pln">   </span><span class="lit">1</span><span
                class="pln">    </span><span class="lit">1</span><span class="pln">
 </span><span class="lit">2</span><span class="pln">   </span><span class="lit">4</span><span
                class="pln">    </span><span class="lit">8</span><span class="pln">
 </span><span class="lit">3</span><span class="pln">   </span><span class="lit">9</span><span
                class="pln">   </span><span class="lit">27</span><span class="pln">
 </span><span class="lit">4</span><span class="pln">  </span><span class="lit">16</span><span
                class="pln">   </span><span class="lit">64</span><span class="pln">
 </span><span class="lit">5</span><span class="pln">  </span><span class="lit">25</span><span class="pln">  </span><span
                class="lit">125</span><span class="pln">
 </span><span class="lit">6</span><span class="pln">  </span><span class="lit">36</span><span class="pln">  </span><span
                class="lit">216</span><span class="pln">
 </span><span class="lit">7</span><span class="pln">  </span><span class="lit">49</span><span class="pln">  </span><span
                class="lit">343</span><span class="pln">
 </span><span class="lit">8</span><span class="pln">  </span><span class="lit">64</span><span class="pln">  </span><span
                class="lit">512</span><span class="pln">
 </span><span class="lit">9</span><span class="pln">  </span><span class="lit">81</span><span class="pln">  </span><span
                class="lit">729</span><span class="pln">
</span><span class="lit">10</span><span class="pln"> </span><span class="lit">100</span><span class="pln"> </span><span
                class="lit">1000</span></pre>
        <p><b>注意：</b>在第一个例子中, 每列间的空格由 print() 添加。</p>
        <p>
            这个例子展示了字符串对象的 rjust() 方法, 它可以将字符串靠右, 并在左边填充空格。</p>
        <p>
            还有类似的方法, 如 ljust() 和 center()。 这些方法并不会写任何东西, 它们仅仅返回新的字符串。</p>
        <p>
            另一个方法 zfill(), 它会在数字的左边填充 0，如下所示：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="str">'12'</span><span class="pun">.</span><span
                class="pln">zfill</span><span class="pun">(</span><span class="lit">5</span><span
                class="pun">)</span><span class="pln">
</span><span class="str">'00012'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="str">'-3.14'</span><span class="pun">.</span><span
                class="pln">zfill</span><span class="pun">(</span><span class="lit">7</span><span
                class="pun">)</span><span class="pln">
</span><span class="str">'-003.14'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="str">'3.14159265359'</span><span
                class="pun">.</span><span class="pln">zfill</span><span class="pun">(</span><span
                class="lit">5</span><span class="pun">)</span><span class="pln">
</span><span class="str">'3.14159265359'</span></pre>
        <p>str.format() 的基本使用如下:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">print</span><span class="pun">(</span><span class="str">'{}网址： "{}!"'</span><span
                class="pun">.</span><span class="pln">format</span><span class="pun">(</span><span
                class="str">'菜鸟教程'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'www.runoob.com'</span><span
                class="pun">))</span><span class="pln">
</span><span class="pun">菜鸟教程网址：</span><span class="pln"> </span><span class="str">"www.runoob.com!"</span></pre>
        <p>括号及其里面的字符 (称作格式化字段) 将会被 format() 中的参数替换。 </p>
        <p>在括号中的数字用于指向传入对象在 format() 中的位置，如下所示：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">print</span><span class="pun">(</span><span class="str">'{0} 和 {1}'</span><span
                class="pun">.</span><span class="pln">format</span><span class="pun">(</span><span
                class="str">'Google'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Runoob'</span><span
                class="pun">))</span><span class="pln">
</span><span class="typ">Google</span><span class="pln"> </span><span class="pun">和</span><span
                class="pln"> </span><span class="typ">Runoob</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span
                class="pun">(</span><span class="str">'{1} 和 {0}'</span><span class="pun">.</span><span class="pln">format</span><span
                class="pun">(</span><span class="str">'Google'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'Runoob'</span><span class="pun">))</span><span class="pln">
</span><span class="typ">Runoob</span><span class="pln"> </span><span class="pun">和</span><span
                class="pln"> </span><span class="typ">Google</span></pre>
        <p>
            如果在 format() 中使用了关键字参数, 那么它们的值会指向使用该名字的参数。</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">print</span><span class="pun">(</span><span class="str">'{name}网址： {site}'</span><span
                class="pun">.</span><span class="pln">format</span><span class="pun">(</span><span
                class="pln">name</span><span class="pun">=</span><span class="str">'菜鸟教程'</span><span
                class="pun">,</span><span class="pln"> site</span><span class="pun">=</span><span class="str">'www.runoob.com'</span><span
                class="pun">))</span><span class="pln">
</span><span class="pun">菜鸟教程网址：</span><span class="pln"> www</span><span class="pun">.</span><span
                class="pln">runoob</span><span class="pun">.</span><span class="pln">com</span></pre>
        <p>
            位置及关键字参数可以任意的结合:
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">print</span><span class="pun">(</span><span class="str">'站点列表 {0}, {1}, 和 {other}。'</span><span
                class="pun">.</span><span class="pln">format</span><span class="pun">(</span><span
                class="str">'Google'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Runoob'</span><span
                class="pun">,</span><span class="pln">
                                                       other</span><span class="pun">=</span><span
                class="str">'Taobao'</span><span class="pun">))</span><span class="pln">
</span><span class="pun">站点列表</span><span class="pln"> </span><span class="typ">Google</span><span
                class="pun">,</span><span class="pln"> </span><span class="typ">Runoob</span><span
                class="pun">,</span><span class="pln"> </span><span class="pun">和</span><span class="pln"> </span><span
                class="typ">Taobao</span><span class="pun">。</span></pre>
        '!a' (使用 ascii()), '!s' (使用 str()) 和 '!r' (使用 repr()) 可以用于在格式化某个值之前对其进行转化:
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">import</span><span class="pln"> math
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span
                class="pun">(</span><span class="str">'常量 PI 的值近似为： {}。'</span><span class="pun">.</span><span
                class="pln">format</span><span class="pun">(</span><span class="pln">math</span><span
                class="pun">.</span><span class="pln">pi</span><span class="pun">))</span><span class="pln">
</span><span class="pun">常量</span><span class="pln"> PI </span><span class="pun">的值近似为：</span><span class="pln"> </span><span
                class="lit">3.141592653589793</span><span class="pun">。</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span
                class="pun">(</span><span class="str">'常量 PI 的值近似为： {!r}。'</span><span class="pun">.</span><span
                class="pln">format</span><span class="pun">(</span><span class="pln">math</span><span
                class="pun">.</span><span class="pln">pi</span><span class="pun">))</span><span class="pln">
</span><span class="pun">常量</span><span class="pln"> PI </span><span class="pun">的值近似为：</span><span class="pln"> </span><span
                class="lit">3.141592653589793</span><span class="pun">。</span></pre>
        <p>可选项 ':' 和格式标识符可以跟着字段名。 这就允许对值进行更好的格式化。 下面的例子将 Pi 保留到小数点后三位：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">import</span><span class="pln"> math
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span
                class="pun">(</span><span class="str">'常量 PI 的值近似为 {0:.3f}。'</span><span class="pun">.</span><span
                class="pln">format</span><span class="pun">(</span><span class="pln">math</span><span
                class="pun">.</span><span class="pln">pi</span><span class="pun">))</span><span class="pln">
</span><span class="pun">常量</span><span class="pln"> PI </span><span class="pun">的值近似为</span><span
                class="pln"> </span><span class="lit">3.142</span><span class="pun">。</span></pre>
        <p>
            在 ':' 后传入一个整数, 可以保证该域至少有这么多的宽度。 用于美化表格时很有用。
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> table </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span
                class="str">'Google'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'Runoob'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'Taobao'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">3</span><span class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">for</span><span class="pln"> name</span><span
                class="pun">,</span><span class="pln"> number </span><span class="kwd">in</span><span
                class="pln"> table</span><span class="pun">.</span><span class="pln">items</span><span
                class="pun">():</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     </span><span class="kwd">print</span><span
                class="pun">(</span><span class="str">'{0:10} ==&gt; {1:10d}'</span><span class="pun">.</span><span
                class="pln">format</span><span class="pun">(</span><span class="pln">name</span><span
                class="pun">,</span><span class="pln"> number</span><span class="pun">))</span><span class="pln">
</span><span class="pun">...</span><span class="pln">
</span><span class="typ">Runoob</span><span class="pln">     </span><span class="pun">==&gt;</span><span
                class="pln">          </span><span class="lit">2</span><span class="pln">
</span><span class="typ">Taobao</span><span class="pln">     </span><span class="pun">==&gt;</span><span
                class="pln">          </span><span class="lit">3</span><span class="pln">
</span><span class="typ">Google</span><span class="pln">     </span><span class="pun">==&gt;</span><span
                class="pln">          </span><span class="lit">1</span></pre>
        <p>
            如果你有一个很长的格式化字符串, 而你不想将它们分开, 那么在格式化时通过变量名而非位置会是很好的事情。
        </p>
        <p>最简单的就是传入一个字典, 然后使用方括号 '[]' 来访问键值 :</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> table </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span
                class="str">'Google'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'Runoob'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'Taobao'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">3</span><span class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span
                class="pun">(</span><span class="str">'Runoob: {0[Runoob]:d}; Google: {0[Google]:d}; Taobao: {0[Taobao]:d}'</span><span
                class="pun">.</span><span class="pln">format</span><span class="pun">(</span><span
                class="pln">table</span><span class="pun">))</span><span class="pln">
</span><span class="typ">Runoob</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">2</span><span class="pun">;</span><span class="pln"> </span><span
                class="typ">Google</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">1</span><span class="pun">;</span><span class="pln"> </span><span
                class="typ">Taobao</span><span class="pun">:</span><span class="pln"> </span><span class="lit">3</span></pre>
        <p>
            也可以通过在 table 变量前使用 '**' 来实现相同的功能：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> table </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span
                class="str">'Google'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'Runoob'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'Taobao'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">3</span><span class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span
                class="pun">(</span><span
                class="str">'Runoob: {Runoob:d}; Google: {Google:d}; Taobao: {Taobao:d}'</span><span
                class="pun">.</span><span class="pln">format</span><span class="pun">(**</span><span
                class="pln">table</span><span class="pun">))</span><span class="pln">
</span><span class="typ">Runoob</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">2</span><span class="pun">;</span><span class="pln"> </span><span
                class="typ">Google</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">1</span><span class="pun">;</span><span class="pln"> </span><span
                class="typ">Taobao</span><span class="pun">:</span><span class="pln"> </span><span class="lit">3</span></pre>



        <h2>旧式字符串格式化</h2>
        <p>
            % 操作符也可以实现字符串格式化。 它将左边的参数作为类似 sprintf() 式的格式化字符串, 而将右边的代入, 然后返回格式化后的字符串. 例如:
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">import</span><span class="pln"> math
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span
                class="pun">(</span><span class="str">'常量 PI 的值近似为：%5.3f。'</span><span class="pln"> </span><span
                class="pun">%</span><span class="pln"> math</span><span class="pun">.</span><span
                class="pln">pi</span><span class="pun">)</span><span class="pln">
</span><span class="pun">常量</span><span class="pln"> PI </span><span class="pun">的值近似为：</span><span
                class="lit">3.142</span><span class="pun">。</span></pre>
        <p>
            因为 str.format() 比较新的函数， 大多数的 Python 代码仍然使用 % 操作符。但是因为这种旧式的格式化最终会从该语言中移除, 应该更多的使用 str.format().
        </p>

        <h2>读取键盘输入</h2>
        <p>
            Python提供了 input() 置函数从标准输入读入一行文本，默认的标准输入是键盘。</p>

        <p>input 可以接收一个Python表达式作为输入，并将运算结果返回。
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

str </span><span class="pun">=</span><span class="pln"> input</span><span class="pun">(</span><span
                class="str">"请输入："</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span
                class="str">"你输入的内容是: "</span><span class="pun">,</span><span class="pln"> str</span><span
                class="pun">)</span></pre>
        <p>
            这会产生如下的对应着输入的结果：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">请输入：菜鸟教程</span><span class="pln">
</span><span class="pun">你输入的内容是:</span><span class="pln">  </span><span class="pun">菜鸟教程</span></pre>



        <h2>读和写文件
        </h2>
        <p>
            open() 将会返回一个 file 对象，基本语法格式如下:
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">open</span><span class="pun">(</span><span
                class="pln">filename</span><span class="pun">,</span><span class="pln"> mode</span><span
                class="pun">)</span></pre>
        <ul>
            <li>filename：filename 变量是一个包含了你要访问的文件名称的字符串值。</li>
            <li>mode：mode决定了打开文件的模式：只读，写入，追加等。所有可取值见如下的完全列表。这个参数是非强制的，默认文件访问模式为只读(r)。</li>
        </ul>
        <p>不同模式打开文件的完全列表：</p>
        <table class="reference">
            <tbody>
            <tr>
                <th style="width:10%">模式</th>
                <th>描述</th>
            </tr>
            <tr>
                <td>r</td>
                <td>以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。</td>
            </tr>
            <tr>
                <td>rb</td>
                <td>以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。</td>
            </tr>
            <tr>
                <td>r+</td>
                <td>打开一个文件用于读写。文件指针将会放在文件的开头。</td>
            </tr>
            <tr>
                <td>rb+</td>
                <td>以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。</td>
            </tr>
            <tr>
                <td>w</td>
                <td>打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在，创建新文件。</td>
            </tr>
            <tr>
                <td>wb</td>
                <td>以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在，创建新文件。</td>
            </tr>
            <tr>
                <td>w+</td>
                <td>打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在，创建新文件。</td>
            </tr>
            <tr>
                <td>wb+</td>
                <td>以二进制格式打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在，创建新文件。</td>
            </tr>
            <tr>
                <td>a</td>
                <td>打开一个文件用于追加。如果该文件已存在，文件指针将会放在文件的结尾。也就是说，新的内容将会被写入到已有内容之后。如果该文件不存在，创建新文件进行写入。</td>
            </tr>
            <tr>
                <td>ab</td>
                <td>以二进制格式打开一个文件用于追加。如果该文件已存在，文件指针将会放在文件的结尾。也就是说，新的内容将会被写入到已有内容之后。如果该文件不存在，创建新文件进行写入。</td>
            </tr>
            <tr>
                <td>a+</td>
                <td>打开一个文件用于读写。如果该文件已存在，文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在，创建新文件用于读写。</td>
            </tr>
            <tr>
                <td>ab+</td>
                <td>以二进制格式打开一个文件用于追加。如果该文件已存在，文件指针将会放在文件的结尾。如果该文件不存在，创建新文件用于读写。</td>
            </tr>
            </tbody>
        </table>
        <p>下图很好的总结了这几种模式：</p>
        <p><img src="//www.runoob.com/wp-content/uploads/2013/11/2112205-861c05b2bdbc9c28.png"></p>
        <table class="reference">
            <thead>
            <tr>
                <th style="text-align:center">模式</th>
                <th style="text-align:center">r</th>
                <th style="text-align:center">r+</th>
                <th style="text-align:center">w</th>
                <th style="text-align:center">w+</th>
                <th style="text-align:center">a</th>
                <th style="text-align:center">a+</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td style="text-align:center">读</td>
                <td style="text-align:center">+</td>
                <td style="text-align:center">+</td>
                <td style="text-align:center"></td>
                <td style="text-align:center">+</td>
                <td style="text-align:center"></td>
                <td style="text-align:center">+</td>
            </tr>
            <tr>
                <td style="text-align:center">写</td>
                <td style="text-align:center"></td>
                <td style="text-align:center">+</td>
                <td style="text-align:center">+</td>
                <td style="text-align:center">+</td>
                <td style="text-align:center">+</td>
                <td style="text-align:center">+</td>
            </tr>
            <tr>
                <td style="text-align:center">创建</td>
                <td style="text-align:center"></td>
                <td style="text-align:center"></td>
                <td style="text-align:center">+</td>
                <td style="text-align:center">+</td>
                <td style="text-align:center">+</td>
                <td style="text-align:center">+</td>
            </tr>
            <tr>
                <td style="text-align:center">覆盖</td>
                <td style="text-align:center"></td>
                <td style="text-align:center"></td>
                <td style="text-align:center">+</td>
                <td style="text-align:center">+</td>
                <td style="text-align:center"></td>
                <td style="text-align:center"></td>
            </tr>
            <tr>
                <td style="text-align:center">指针在开始</td>
                <td style="text-align:center">+</td>
                <td style="text-align:center">+</td>
                <td style="text-align:center">+</td>
                <td style="text-align:center">+</td>
                <td style="text-align:center"></td>
                <td style="text-align:center"></td>
            </tr>
            <tr>
                <td style="text-align:center">指针在结尾</td>
                <td style="text-align:center"></td>
                <td style="text-align:center"></td>
                <td style="text-align:center"></td>
                <td style="text-align:center"></td>
                <td style="text-align:center">+</td>
                <td style="text-align:center">+</td>
            </tr>
            </tbody>
        </table>
        <p>以下实例将字符串写入到文件 foo.txt 中：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># 打开一个文件</span><span class="pln">
f </span><span class="pun">=</span><span class="pln"> open</span><span class="pun">(</span><span class="str">"/tmp/foo.txt"</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">"w"</span><span
                class="pun">)</span><span class="pln">

f</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span
                class="pln"> </span><span class="str">"Python 是一个非常好的语言。\n是的，的确非常好!!\n"</span><span class="pln"> </span><span
                class="pun">)</span><span class="pln">

</span><span class="com"># 关闭打开的文件</span><span class="pln">
f</span><span class="pun">.</span><span class="pln">close</span><span class="pun">()</span></pre>

        <ul>
            <li>
                第一个参数为要打开的文件名。
            </li>
            <li>第二个参数描述文件如何使用的字符。 mode 可以是 'r' 如果文件只读, 'w' 只用于写 (如果存在同名文件则将被删除), 和 'a' 用于追加文件内容; 所写的任何数据都会被自动增加到末尾. 'r+'
                同时用于读写。 mode 参数是可选的; 'r' 将是默认值。
            </li>
        </ul>
        <p>
            此时打开文件 foo.txt,显示如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ cat </span><span class="pun">/</span><span
                class="pln">tmp</span><span class="pun">/</span><span class="pln">foo</span><span
                class="pun">.</span><span class="pln">txt
</span><span class="typ">Python</span><span class="pln"> </span><span class="pun">是一个非常好的语言。</span><span class="pln">
</span><span class="pun">是的，的确非常好!!</span></pre>

        <h2>文件对象的方法</h2>
        <p>
            本节中剩下的例子假设已经创建了一个称为 f 的文件对象。
        </p>
        <h3>f.read()</h3>
        <p>
            为了读取一个文件的内容，调用 f.read(size), 这将读取一定数目的数据, 然后作为字符串或字节对象返回。</p>
        <p>size 是一个可选的数字类型的参数。 当 size 被忽略了或者为负, 那么该文件的所有内容都将被读取并且返回。</p>
        <p>以下实例假定文件 foo.txt 已存在（上面实例中已创建）：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># 打开一个文件</span><span class="pln">
f </span><span class="pun">=</span><span class="pln"> open</span><span class="pun">(</span><span class="str">"/tmp/foo.txt"</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">"r"</span><span
                class="pun">)</span><span class="pln">

str </span><span class="pun">=</span><span class="pln"> f</span><span class="pun">.</span><span
                class="pln">read</span><span class="pun">()</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">)</span><span class="pln">

</span><span class="com"># 关闭打开的文件</span><span class="pln">
f</span><span class="pun">.</span><span class="pln">close</span><span class="pun">()</span></pre>
        <p>执行以上程序，输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Python</span><span class="pln"> </span><span
                class="pun">是一个非常好的语言。</span><span class="pln">
</span><span class="pun">是的，的确非常好!!</span></pre>
        <h3>f.readline()</h3>
        <p>
            f.readline() 会从文件中读取单独的一行。换行符为 '\n'。f.readline() 如果返回一个空字符串, 说明已经已经读取到最后一行。</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># 打开一个文件</span><span class="pln">
f </span><span class="pun">=</span><span class="pln"> open</span><span class="pun">(</span><span class="str">"/tmp/foo.txt"</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">"r"</span><span
                class="pun">)</span><span class="pln">

str </span><span class="pun">=</span><span class="pln"> f</span><span class="pun">.</span><span
                class="pln">readline</span><span class="pun">()</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">)</span><span class="pln">

</span><span class="com"># 关闭打开的文件</span><span class="pln">
f</span><span class="pun">.</span><span class="pln">close</span><span class="pun">()</span></pre>
        <p>执行以上程序，输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Python</span><span class="pln"> </span><span
                class="pun">是一个非常好的语言。</span></pre>
        <h3>f.readlines()</h3>
        <p>
            f.readlines() 将返回该文件中包含的所有行。
        </p>
        <p> 如果设置可选参数 sizehint, 则读取指定长度的字节, 并且将这些字节按行分割。
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># 打开一个文件</span><span class="pln">
f </span><span class="pun">=</span><span class="pln"> open</span><span class="pun">(</span><span class="str">"/tmp/foo.txt"</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">"r"</span><span
                class="pun">)</span><span class="pln">

str </span><span class="pun">=</span><span class="pln"> f</span><span class="pun">.</span><span
                class="pln">readlines</span><span class="pun">()</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">)</span><span class="pln">

</span><span class="com"># 关闭打开的文件</span><span class="pln">
f</span><span class="pun">.</span><span class="pln">close</span><span class="pun">()</span></pre>
        <p>执行以上程序，输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">[</span><span class="str">'Python 是一个非常好的语言。\n'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'是的，的确非常好!!\n'</span><span
                class="pun">]</span></pre>
        <p>
            另一种方式是迭代一个文件对象然后读取每行:
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># 打开一个文件</span><span class="pln">
f </span><span class="pun">=</span><span class="pln"> open</span><span class="pun">(</span><span class="str">"/tmp/foo.txt"</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">"r"</span><span
                class="pun">)</span><span class="pln">

</span><span class="kwd">for</span><span class="pln"> line </span><span class="kwd">in</span><span class="pln"> f</span><span
                class="pun">:</span><span class="pln">
    </span><span class="kwd">print</span><span class="pun">(</span><span class="pln">line</span><span
                class="pun">,</span><span class="pln"> </span><span class="kwd">end</span><span
                class="pun">=</span><span class="str">''</span><span class="pun">)</span><span class="pln">

</span><span class="com"># 关闭打开的文件</span><span class="pln">
f</span><span class="pun">.</span><span class="pln">close</span><span class="pun">()</span></pre>
        <p>执行以上程序，输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Python</span><span class="pln"> </span><span
                class="pun">是一个非常好的语言。</span><span class="pln">
</span><span class="pun">是的，的确非常好!!</span></pre>
        <p>
            这个方法很简单, 但是并没有提供一个很好的控制。 因为两者的处理机制不同, 最好不要混用。
        </p>
        <h3>f.write()</h3>
        <p>
            f.write(string) 将 string 写入到文件中, 然后返回写入的字符数。
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># 打开一个文件</span><span class="pln">
f </span><span class="pun">=</span><span class="pln"> open</span><span class="pun">(</span><span class="str">"/tmp/foo.txt"</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">"w"</span><span
                class="pun">)</span><span class="pln">

num </span><span class="pun">=</span><span class="pln"> f</span><span class="pun">.</span><span class="pln">write</span><span
                class="pun">(</span><span class="pln"> </span><span class="str">"Python 是一个非常好的语言。\n是的，的确非常好!!\n"</span><span
                class="pln"> </span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">num</span><span
                class="pun">)</span><span class="pln">
</span><span class="com"># 关闭打开的文件</span><span class="pln">
f</span><span class="pun">.</span><span class="pln">close</span><span class="pun">()</span></pre>
        <p>执行以上程序，输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">29</span></pre>
        <p>
            如果要写入一些不是字符串的东西, 那么将需要先进行转换:
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># 打开一个文件</span><span class="pln">
f </span><span class="pun">=</span><span class="pln"> open</span><span class="pun">(</span><span class="str">"/tmp/foo1.txt"</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">"w"</span><span
                class="pun">)</span><span class="pln">

value </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="str">'www.runoob.com'</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">14</span><span class="pun">)</span><span
                class="pln">
s </span><span class="pun">=</span><span class="pln"> str</span><span class="pun">(</span><span class="pln">value</span><span
                class="pun">)</span><span class="pln">
f</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span
                class="pln">s</span><span class="pun">)</span><span class="pln">

</span><span class="com"># 关闭打开的文件</span><span class="pln">
f</span><span class="pun">.</span><span class="pln">close</span><span class="pun">()</span></pre>
        <p>执行以上程序，打开 foo1.txt 文件：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ cat </span><span class="pun">/</span><span
                class="pln">tmp</span><span class="pun">/</span><span class="pln">foo1</span><span
                class="pun">.</span><span class="pln">txt
</span><span class="pun">(</span><span class="str">'www.runoob.com'</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">14</span><span class="pun">)</span></pre>
        <h3>f.tell()</h3>
        <p>
            f.tell() 返回文件对象当前所处的位置, 它是从文件开头开始算起的字节数。
        </p>
        <h3>f.seek()</h3>
        <p>如果要改变文件当前的位置, 可以使用 f.seek(offset, from_what) 函数。</p>
        <p>
            from_what 的值, 如果是 0 表示开头, 如果是 1 表示当前位置, 2 表示文件的结尾，例如：</p>
        <p></p>
        <ul>
            <li>
                seek(x,0) ： 从起始位置即文件首行首字符开始移动 x 个字符
            </li>
            <li>
                seek(x,1) ： 表示从当前位置往后移动x个字符
            </li>
            <li>
                seek(-x,2)：表示从文件的结尾往前移动x个字符
            </li>
        </ul>
        <p>
            from_what 值为默认为0，即文件开头。下面给出一个完整的例子：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> f </span><span class="pun">=</span><span class="pln"> open</span><span class="pun">(</span><span
                class="str">'/tmp/foo.txt'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'rb+'</span><span
                class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> f</span><span class="pun">.</span><span
                class="pln">write</span><span class="pun">(</span><span class="pln">b</span><span class="str">'0123456789abcdef'</span><span
                class="pun">)</span><span class="pln">
</span><span class="lit">16</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> f</span><span class="pun">.</span><span
                class="pln">seek</span><span class="pun">(</span><span class="lit">5</span><span
                class="pun">)</span><span class="pln">     </span><span class="com"># 移动到文件的第六个字节</span><span
                class="pln">
</span><span class="lit">5</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> f</span><span class="pun">.</span><span
                class="pln">read</span><span class="pun">(</span><span class="lit">1</span><span
                class="pun">)</span><span class="pln">
b</span><span class="str">'5'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> f</span><span class="pun">.</span><span
                class="pln">seek</span><span class="pun">(-</span><span class="lit">3</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span class="pun">)</span><span
                class="pln"> </span><span class="com"># 移动到文件的倒数第三字节</span><span class="pln">
</span><span class="lit">13</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> f</span><span class="pun">.</span><span
                class="pln">read</span><span class="pun">(</span><span class="lit">1</span><span
                class="pun">)</span><span class="pln">
b</span><span class="str">'d'</span></pre>
        <p>
        </p>
        <h3>f.close()</h3>
        在文本文件中 (那些打开文件的模式下没有 b 的), 只会相对于文件起始位置进行定位。<p></p>
        <p>
            当你处理完一个文件后, 调用 f.close() 来关闭文件并释放系统的资源，如果尝试再调用该文件，则会抛出异常。</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> f</span><span
                class="pun">.</span><span class="pln">close</span><span class="pun">()</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> f</span><span class="pun">.</span><span
                class="pln">read</span><span class="pun">()</span><span class="pln">
</span><span class="typ">Traceback</span><span class="pln"> </span><span class="pun">(</span><span class="pln">most recent call </span><span
                class="kwd">last</span><span class="pun">):</span><span class="pln">
  </span><span class="typ">File</span><span class="pln"> </span><span class="str">"&lt;stdin&gt;"</span><span
                class="pun">,</span><span class="pln"> line </span><span class="lit">1</span><span
                class="pun">,</span><span class="pln"> </span><span class="kwd">in</span><span class="pln"> </span><span
                class="pun">?</span><span class="pln">
</span><span class="typ">ValueError</span><span class="pun">:</span><span class="pln"> I</span><span
                class="pun">/</span><span class="pln">O operation on closed file
</span><span class="str">&lt;pre&gt;</span><span class="pln">
</span><span class="str">&lt;p&gt;</span><span class="pln">
</span><span class="pun">当处理一个文件对象时,</span><span class="pln"> </span><span class="pun">使用</span><span
                class="pln"> </span><span class="kwd">with</span><span class="pln"> </span><span class="pun">关键字是非常好的方式。在结束后,</span><span
                class="pln"> </span><span class="pun">它会帮你正确的关闭文件。</span><span class="pln"> </span><span class="pun">而且写起来也比</span><span
                class="pln"> </span><span class="kwd">try</span><span class="pln"> </span><span
                class="pun">-</span><span class="pln"> </span><span class="kwd">finally</span><span class="pln"> </span><span
                class="pun">语句块要简短:&lt;/</span><span class="pln">p</span><span class="pun">&gt;</span><span class="pln">
</span><span class="str">&lt;pre&gt;</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">with</span><span class="pln"> open</span><span
                class="pun">(</span><span class="str">'/tmp/foo.txt'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'r'</span><span class="pun">)</span><span
                class="pln"> </span><span class="kwd">as</span><span class="pln"> f</span><span
                class="pun">:</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     read_data </span><span class="pun">=</span><span class="pln"> f</span><span
                class="pun">.</span><span class="pln">read</span><span class="pun">()</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> f</span><span class="pun">.</span><span class="pln">closed
</span><span class="kwd">True</span></pre>
        <p>
            文件对象还有其他方法, 如 isatty() 和 trucate(), 但这些通常比较少用。</p>


        <h2>pickle 模块
        </h2>
        <p>
            python的pickle模块实现了基本的数据序列和反序列化。</p>
        <p>通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去，永久存储。</p>
        <p>通过pickle模块的反序列化操作，我们能够从文件中创建上一次程序保存的对象。
        </p>
        <p>基本接口：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">pickle</span><span class="pun">.</span><span
                class="kwd">dump</span><span class="pun">(</span><span class="pln">obj</span><span
                class="pun">,</span><span class="pln"> file</span><span class="pun">,</span><span
                class="pln"> </span><span class="pun">[,</span><span class="pln">protocol</span><span
                class="pun">])</span></pre>
        <p>
            有了 pickle 这个对象, 就能对 file 以读取的形式打开:
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">x </span><span class="pun">=</span><span
                class="pln"> pickle</span><span class="pun">.</span><span class="pln">load</span><span
                class="pun">(</span><span class="pln">file</span><span class="pun">)</span></pre>
        <p><b>注解：</b>从 file 中读取一个字符串，并将它重构为原来的python对象。</p>
        <p><b>file:</b> 类文件对象，有read()和readline()接口。</p>
        <p>实例1：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> pickle

</span><span class="com"># 使用pickle模块将数据对象保存到文件</span><span class="pln">
data1 </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span
                class="str">'a'</span><span class="pun">:</span><span class="pln"> </span><span
                class="pun">[</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">2.0</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">3</span><span class="pun">,</span><span class="pln"> </span><span class="lit">4</span><span
                class="pun">+</span><span class="lit">6j</span><span class="pun">],</span><span class="pln">
         </span><span class="str">'b'</span><span class="pun">:</span><span class="pln"> </span><span
                class="pun">(</span><span class="str">'string'</span><span class="pun">,</span><span
                class="pln"> u</span><span class="str">'Unicode string'</span><span class="pun">),</span><span
                class="pln">
         </span><span class="str">'c'</span><span class="pun">:</span><span class="pln"> </span><span
                class="kwd">None</span><span class="pun">}</span><span class="pln">

selfref_list </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span
                class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">3</span><span class="pun">]</span><span
                class="pln">
selfref_list</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="pln">selfref_list</span><span
                class="pun">)</span><span class="pln">

output </span><span class="pun">=</span><span class="pln"> open</span><span class="pun">(</span><span class="str">'data.pkl'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'wb'</span><span
                class="pun">)</span><span class="pln">

</span><span class="com"># Pickle dictionary using protocol 0.</span><span class="pln">
pickle</span><span class="pun">.</span><span class="kwd">dump</span><span class="pun">(</span><span
                class="pln">data1</span><span class="pun">,</span><span class="pln"> output</span><span
                class="pun">)</span><span class="pln">

</span><span class="com"># Pickle the list using the highest protocol available.</span><span class="pln">
pickle</span><span class="pun">.</span><span class="kwd">dump</span><span class="pun">(</span><span class="pln">selfref_list</span><span
                class="pun">,</span><span class="pln"> output</span><span class="pun">,</span><span class="pln"> </span><span
                class="pun">-</span><span class="lit">1</span><span class="pun">)</span><span class="pln">

output</span><span class="pun">.</span><span class="pln">close</span><span class="pun">()</span></pre>
        <p>实例2：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> pprint</span><span class="pun">,</span><span class="pln"> pickle

</span><span class="com">#使用pickle模块从文件中重构python对象</span><span class="pln">
pkl_file </span><span class="pun">=</span><span class="pln"> open</span><span class="pun">(</span><span class="str">'data.pkl'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'rb'</span><span
                class="pun">)</span><span class="pln">

data1 </span><span class="pun">=</span><span class="pln"> pickle</span><span class="pun">.</span><span
                class="pln">load</span><span class="pun">(</span><span class="pln">pkl_file</span><span
                class="pun">)</span><span class="pln">
pprint</span><span class="pun">.</span><span class="pln">pprint</span><span class="pun">(</span><span
                class="pln">data1</span><span class="pun">)</span><span class="pln">

data2 </span><span class="pun">=</span><span class="pln"> pickle</span><span class="pun">.</span><span
                class="pln">load</span><span class="pun">(</span><span class="pln">pkl_file</span><span
                class="pun">)</span><span class="pln">
pprint</span><span class="pun">.</span><span class="pln">pprint</span><span class="pun">(</span><span
                class="pln">data2</span><span class="pun">)</span><span class="pln">

pkl_file</span><span class="pun">.</span><span class="pln">close</span><span class="pun">()</span></pre>

    </div>
</div>
</body>
</html>
